<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use Myredis\Myredis;

class Link extends Admincontroller
{
    /**
     *显示用户列表
     * @return \think\Response
     */
    public function index()
    {
        $list = Db::table('xo_admin_link')->field(true)->select();
        return view('link/index',['list'=>$list]);
    }

    /**
     * 后台会员激活/禁用操作
     * @return \think\response\Json|void
     */
    public function action()
    {
        if (!Request::instance()->isAjax()) {
            return $this->error('小伙子迷了路', url('admin/index/index'));
        }
        $id = Request::instance()->get('id');
        $status = Db::table('xo_admin_link')->where('id',$id)->field('status')->find();
        if ($status['status'] == 1) {
            $da['status']=0;
        $res = Db::table('xo_admin_link')->where(['id' => $id])->update($da);
        if (!$res) {
            $data = [
                    'status' => 0,
                    'info' => '网络错误'
                ];
            return json($data);
        }
            $data = [
                    'status' => 0,
                    'info' => '友链' . $id . '禁用成功'
                ];
            return json($data);
        }else{
            $da['status']=1;
        $res = Db::table('xo_admin_link')->where(['id' => $id])->update($da);
        $data = [
                'status' => 1,
                'info' => '友链' . $id . '激活成功'
            ];
            return json($data);
        }
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('link/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = input('post.');
        if (empty($data['name'])) {
            return $this->error('名字不能为空哦~再去试试');
        }
        $res = Db::table('xo_admin_link')->where('name',$data['name'])->select();
        if (!empty($res)) {
            return $this->error('名字重复啦~ 换个再试试吧');
        }

        if ((in_array("on",$data))) {
            $data['status'] ='1';
        }else{
            $data['status'] ='0';
        }
        // var_dump($data);die;
        $data['id'] ='';
        $data['create_time'] = date('Y-m-d H:i:s');
        $result = Db::table('xo_admin_link')->data($data)->insert();
        if ($result > 0) {
            return $this->success('新增成功了~~', url('admin/link/index'));
        } else {
            return $this->error('新增失败~~');
        }    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!Request::instance()->isAjax()) {
            return $this->error('小伙子迷了路', url('admin/index/index'));
        }
        $res = Db::table('xo_user')->field('pwd', true)->find($id);
        if ($res) {
            return json($res);
        } else {
            return null;
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        echo 1;
        // $res = Db::table('xo_user')->field('pwd', true)->find($id);
        // if ($res) {
        //     return view('edit', [
        //         'title' => '编辑用户',
        //         'res' => $res
        //     ]);
        // } else {
        //     return $this->error('网络错误.......');
        // }

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        echo 1;
        // if (!Request::instance()->put()) {
        //     return $this->error('小伙子是不是迷了路');
        // }
        // $id = Request::instance()->put('id');
        // $data = [
        //     'nickname' => Request::instance()->put('nickname'),
        //     'sign' => Request::instance()->put('sign'),
        //     'sex' => Request::instance()->put('sex'),
        //     'birthday' => Request::instance()->put('birthday'),
        //     'email' => Request::instance()->put('email'),
        //     'qq' => Request::instance()->put('qq'),
        // ];
        // $res = Db::table('xo_user')->where(['id' => $id])->update($data);
        // if ($res) {
        //     return $this->success('编辑成功', url('admin/member/index'));
        // } else {
        //     return $this->error('编辑失败');
        // }
    }




   /**
     * 加载友链编辑的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function link_update($id)
    {
        $list = Db::table('xo_admin_link')->where('id',$id)->find();
        // var_dump($list);
        return view('link/edit',['list'=>$list]);
    }

   /**
     * 执行友链编辑
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function do_link()
    {
        $data = input('post.');
        $res = Db::table('xo_admin_link')->where(['id' => $data['id']])->update($data);
        if ($res) {
            return $this->success('编辑成功啦~~~',url('admin/link/index'));
        }else{
            return $this->error('编辑失败,请重试~~');
        }
    }

    /**
     * 删除指定资源
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {

        if (!Request::instance()->isDelete()) {
            return $this->error('小伙子,迷了路....', url('admin/index/index'));
        }

        $res = Db::table('xo_admin_link')->where(['id' => $id])->delete();
        if ($res) {
            $data = [
                'status' => true,
                'info' => '友链' . $id . '已经被删除成功'
            ];
        } else {
            $data = [
                'status' => false,
                'status' => '友链' . $id . '删除失败'
            ];
        }
        return json($data);
    }
}
